package com.cee.admin.config.swagger;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.customizers.PropertyCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;


@Configuration
@ConditionalOnProperty(prefix = "spring-doc.api-docs",value = {"enabled"},havingValue = "true")
public class SwaggerConfig {

    @Bean
    public GlobalOpenApiCustomizer globalOpenApiCustomizer() {
        return openApi -> {
            // 配置鉴权参数
            if (openApi.getPaths() != null) {
                openApi.getPaths().forEach((s, pathItem) -> {
                    // 登录接口/验证码不需要添加鉴权参数
                    if ("/auth/login".equals(s) || "/auth/login/novalid".equals(s) || "/auth/code/image".equals(s)) {
                        return;
                    }
                    // 接口添加鉴权参数
                    pathItem.readOperations()
                            .forEach(operation ->
                                    operation.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
                            );
                });
            }
        };
    }


    // 设置 openapi 基础参数
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("碳E算-管理端接口文档")
                        .version("1.0")
                        .description("注意事项：1、计算统一用精确计算，如无特别说明，保留6位小数，四舍五入  2、对于字段长度、大小、是否必须、正则这种字段约束，可能knife4k显示不完全，可查看”/swagger-ui.html“页面"))
                // 配置鉴权参数
                .components(new Components()
                        .addSecuritySchemes(HttpHeaders.AUTHORIZATION,
                                new SecurityScheme()
                                        .name("X-TOKEN")
                                        .type(SecurityScheme.Type.APIKEY)
                                        .in(SecurityScheme.In.HEADER)
                                        .scheme("Bearer")
                                        .bearerFormat("JWT")
                        )
                );
    }

    /**
     * propertyCustomizer
     * 增加对title的支持
     * @return propertyCustomizer
     */
    @Bean
    public PropertyCustomizer propertyCustomizer() {

        return (schema, type) -> {

            String description = schema.getDescription();
            String title = schema.getTitle();
            if (null == description || description.length() == 0) {
                schema.description(title);
            } else {
                if (title != null && title.length() != 0) {
                    schema.description(schema.getTitle() + " (" + description + ") ");
                }
            }
            return schema;
        };
    }
}
